package com.robaone.gwt.hierarcialdb.client.ui.admin;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.robaone.gwt.hierarcialdb.client.data.DataConstants;
import com.robaone.gwt.hierarcialdb.client.state.AdminState;

public class FileUi extends Composite {
	private static FileUiUiBinder uiBinder = GWT.create(FileUiUiBinder.class);
	
	private String type;
	private String id;

	interface FileUiUiBinder extends UiBinder<Widget, FileUi> {
	}

	public FileUi(){
		initWidget(uiBinder.createAndBindUi(this));
	}
	public void setValues(String filename,String type,String id){
		this.id = id;
		this.type = type;
		Resources res =  GWT.create(Resources.class);
		int ftype = Integer.parseInt(type);
		this.filename.setText(filename);
		if(ftype == DataConstants.STRING){
			icon.setResource(res.string_file());
		}else if(ftype == DataConstants.INT){
			icon.setResource(res.number_file());
		}else if(ftype == DataConstants.DOUBLE){
			icon.setResource(res.number_file());
		}else if(ftype == DataConstants.BOOLEAN){
			icon.setResource(res.bit_file());
		}else if(ftype == DataConstants.DATETIME){
			icon.setResource(res.date_file());
		}else if(ftype == DataConstants.TEXT){
			icon.setResource(res.text_file());
		}else if(ftype == DataConstants.BINARY){
			icon.setResource(res.binary_file());
		}else if(ftype == DataConstants.JSON){
			icon.setResource(res.json_file());
		}
	}
	public FileUi(String filename,String type,String id) {
		initWidget(uiBinder.createAndBindUi(this));
		this.setValues(filename,type,id);
	}

	@UiField Image icon;
	@UiField Label filename;
	
	@UiHandler("icon")
	public void handleIconClick(ClickEvent event){
		AdminState.showFileContents(this.id);
	}
}
